import unittest
from unittest.mock import patch

from pyecharts import options as opts
from pyecharts.charts import Kline

data = [
    [2320.26, 2320.26, 2287.3, 2362.94],
    [2300, 2291.3, 2288.26, 2308.38],
    [2295.35, 2346.5, 2295.35, 2345.92],
    [2347.22, 2358.98, 2337.35, 2363.8],
    [2360.75, 2382.48, 2347.89, 2383.76],
    [2383.43, 2385.42, 2371.23, 2391.82],
    [2377.41, 2419.02, 2369.57, 2421.15],
    [2425.92, 2428.15, 2417.58, 2440.38],
    [2411, 2433.13, 2403.3, 2437.42],
    [2432.68, 2334.48, 2427.7, 2441.73],
]


class TestKlineChart(unittest.TestCase):
    @patch("pyecharts.render.engine.write_utf8_html_file")
    def test_kline_base(self, fake_writer):
        c = (
            Kline()
            .add_xaxis(["2017/7/{}".format(i + 1) for i in range(10)])
            .add_yaxis("kline", data)
            .set_global_opts(
                yaxis_opts=opts.AxisOpts(is_scale=True),
                xaxis_opts=opts.AxisOpts(is_scale=True),
            )
        )
        c.render()
        _, content = fake_writer.call_args[0]
        self.assertEqual(c.theme, "white")
        self.assertEqual(c.renderer, "canvas")

    @patch("pyecharts.render.engine.write_utf8_html_file")
    def test_kline_item_base(self, fake_writer):
        x_axis = ["2017/7/{}".format(i + 1) for i in range(10)]
        y_axis = data
        kline_item = [
            opts.CandleStickItem(name=d[0], value=d[1])
            for d in list(zip(x_axis, y_axis))
        ]

        c = (
            Kline()
            .add_xaxis(x_axis)
            .add_yaxis("kline", kline_item)
            .set_global_opts(
                yaxis_opts=opts.AxisOpts(is_scale=True),
                xaxis_opts=opts.AxisOpts(is_scale=True),
            )
        )
        c.render()
        _, content = fake_writer.call_args[0]
        self.assertEqual(c.theme, "white")
        self.assertEqual(c.renderer, "canvas")

    @patch("pyecharts.render.engine.write_utf8_html_file")
    def test_kline_axispointer_opts(self, fake_writer):
        c = (
            Kline()
            .add_xaxis(["2017/7/{}".format(i + 1) for i in range(10)])
            .add_yaxis("kline", data)
            .set_global_opts(
                yaxis_opts=opts.AxisOpts(is_scale=True),
                xaxis_opts=opts.AxisOpts(is_scale=True),
                axispointer_opts=opts.AxisPointerOpts(
                    is_show=True,
                    link=[{"xAxisIndex": "all"}],
                    label=opts.LabelOpts(background_color="#777"),
                ),
            )
        )
        c.render()
        _, content = fake_writer.call_args[0]
        self.assertIn("axisPointer", content)
